<!DOCTYPE html>
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Visibility Test</title>
    <script src="../../../webcomponentsjs/webcomponents.js"></script>
    <link rel="import" href="../../polymer.html">
    <script src="../../../tools/test/htmltest.js"></script>
    <script src="../../../tools/test/chai/chai.js"></script>
  </head>
  <body>
    <template is="auto-binding">
      <span>{{value}}</span>
      <a href="popup.html" target="popup">Open Popup</a>
    </template>
    <script>
      var t = document.querySelector('template');
      t.addEventListener('template-bound', function() {
        if (Observer.hasObjectObserve) {
          t.model.value = 'test skipped due to Object.observe';
          done();
        } else {
          t.model.value = 'a';
          var s = document.querySelector('span');
          var a = document.querySelector('a');
          document.addEventListener('visibilitychange', function(){
            console.log('visibility change!');
          });
          window.addEventListener('message', function(ev) {
            if (ev.data === 'popup-initialized') {
              console.log('got popup initialized');
              t.model.value = 'b';
              setTimeout(function() {
                chai.assert.equal(document.hidden, true);
                chai.assert.equal(s.textContent, 'a', 'change did not occur, and shouldn\'t');
                console.log('send close');
                ev.source.postMessage('close', location.origin);
              }, 300);
            }
            if (ev.data === 'popup-closed') {
              setTimeout(function() {
                chai.assert.equal(s.textContent, 'b', 'change did occur, and should');
                done();
              }, 300);
            }
          });
        }
      });
    </script>
  </body>
</html>
